package cn.edu.guet.controller;

import cn.edu.guet.bean.personimf.*;
import cn.edu.guet.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class personimfController {
    @Autowired
    private cn.edu.guet.service.personifm.personimfseritf personimfseritf;
    @RequestMapping("html/personimfhtml/mainperson.do")
    public String mainperson(HttpServletRequest request){
        String userid=""+request.getSession().getAttribute("userid");
        request.setAttribute("userid",userid);
        return "html/personimfhtml/mainperson";
    }
    @RequestMapping("html/personimfhtml/yemian.do")
    public String yemian(String yemian,String userid,HttpServletRequest request){
        System.out.println(yemian);
        request.setAttribute("userid",userid);
        return "html/personimfhtml/"+yemian;
    }


    @RequestMapping("html/personimfhtml/cmapplied.do")
    public String cmapplied(HttpServletRequest request){
        String userid=""+request.getSession().getAttribute("userid");
        request.setAttribute("userid",userid);
        return "html/personimfhtml/cmapplied";
    }



    @RequestMapping("html/personimfhtml/organxingxi.do")
    @ResponseBody
    public List<dgroup_lj> getdgroup(String userid){
        int gcount=personimfseritf.getgcount(userid);
        List<dgroup_lj>list=personimfseritf.getdgroup(userid);
        if(list.size()!=0) {
            list.get(0).setGcount(gcount + "");
        }
        return list;
    }
    @RequestMapping("html/personimfhtml/grouppeople.do")
    @ResponseBody
    public List<grouppeople>getgrouppeople(String userid, int page){
        grouppeople grouppeople=new grouppeople();
        grouppeople.setUserid(userid);
        grouppeople.setPage(page);
        List<cn.edu.guet.bean.personimf.grouppeople>list=personimfseritf.getgrouppeople(grouppeople);
        if(list.size()!=0){
            list.get(0).setGcount(personimfseritf.getgcount(userid));
        }
        return list;
    }
    @RequestMapping("html/personimfhtml/getpeopleById.do")
    @ResponseBody
    public List<grouppeople>getpeopleById(String userid, String username){
        List<grouppeople>list=personimfseritf.getpeopleById(userid,username);
        return list;
    }
    @RequestMapping("html/personimfhtml/getUser.do")
    @ResponseBody
    public Users getUser(String userid){
        Users users=personimfseritf.getUser(userid);
        return users;
    }
    @RequestMapping("html/personimfhtml/ChangeUser.do")
    @ResponseBody
    public int  ChangeUser(String user){
        String[]arr=user.split(",");
        Users users=new Users();
        users.setAge(arr[0]);
        users.setPhone(arr[1]);
        users.setEmail(arr[2]);
        users.setMname(arr[3]);
        users.setGrade(arr[4]);
        users.setCname(arr[5]);
        users.setUserid(arr[6]);
        personimfseritf.changeUser(users);
        return 1;

    }
    @RequestMapping("html/personimfhtml/getMajor.do")
    @ResponseBody
    public int getMajor(String mname){
        Macollege macollege=personimfseritf.getMajor(mname);
        if (macollege==null){
            return 0;
        }
        else {
            return 1;
        }

    }
    @RequestMapping("html/personimfhtml/getCollege.do")
    @ResponseBody
    public int getCollege(String cname){
        college college=personimfseritf.getCollege(cname);
        if (college==null){
            return 0;
        }
        else {
            return 1;
        }

    }
    @RequestMapping("html/personimfhtml/getMacollege.do")
    @ResponseBody
    public int getMacollege(String mname,String cname ){
        Macollege macollege=personimfseritf.getMacollege(mname,cname);
        if (macollege==null){
            return 0;
        }
        else {
            return 1;
        }

    }
    @RequestMapping("html/personimfhtml/checkOldPassword.do")
    @ResponseBody
    public int checkOldPassword(String userid,String password){
        MD5 md5=new MD5();
        String newpassword=null;
        newpassword=md5.encryptionMD5(password);
        Users users=personimfseritf.checkOldPassword(userid,newpassword);
        if (users==null){
            return 0;
        }
        else {
            return 1;
        }

    }
    @RequestMapping("html/personimfhtml/checkEmail.do")
    @ResponseBody
    public int checkEmail(String userid,String email){
        Users users=personimfseritf.checkEmail(userid,email);
        if (users==null){
            return 0;
        }
        else {
            return 1;
        }
    }
    @RequestMapping("html/personimfhtml/changePassword.do")
    @ResponseBody
    public int  changePassword(String userid,String password){
        MD5 md5=new MD5();
        String newpassword=md5.encryptionMD5(password);
        personimfseritf.changePassword(userid,newpassword);
        return 1;
    }
    @RequestMapping("html/personimfhtml/showGroup.do")
    @ResponseBody
    public List<dgroup_lj>showGroup(int page){
        int count=personimfseritf.countGroup();
        List<dgroup_lj>list=personimfseritf.showGroup(page);
        for(int i=0;i<list.size();i++){
            list.get(i).setCount(count);
            int gcount=personimfseritf.getgcountBygid(list.get(i).getGid());
            list.get(i).setGcount(gcount+"");
        }
        return list;
    }
    @RequestMapping("html/personimfhtml/outgroup.do")
    public String  outgroup(String userid)  {
        personimfseritf.outgroup(userid);
        return "forward:organxingxi.do?userid="+userid;
    }
    @RequestMapping("html/personimfhtml/getgroupById.do")
    @ResponseBody
    public List<dgroup_lj> getgroupById(String gid){
        int gcount=personimfseritf.getgcountBygid(gid);
        List<dgroup_lj>list=personimfseritf.getgroupById(gid);
        if (list.size()!=0) {
            list.get(0).setGcount(gcount + "");
        }
        return list;
    }

    @RequestMapping("html/personimfhtml/creategroup.do")
    @ResponseBody
    public int creategroup(String group){
        String newgroup[]=group.split(",");
        dgroup_lj dgroup=new dgroup_lj();
        dgroup.setGid(newgroup[0]);
        dgroup.setGname(newgroup[1]);
        personimfseritf.creategroup(dgroup);
        personimfseritf.updataadm(newgroup[2],newgroup[0]);
        return 1;
    }
    @RequestMapping("html/personimfhtml/updataimg.do")
    @ResponseBody
    public int groupdata(String userid, MultipartFile file){
        String fileType = file.getContentType();
        if (fileType.equals("image/jpeg") || fileType.equals("image/png") || fileType.equals("image/jpeg")) {
            String fileName = file.getOriginalFilename();

            //获取文件后缀名
            int index = fileName.lastIndexOf(".");
            String suffixName;
            if(index > 0) {
                suffixName = fileName.substring(fileName.lastIndexOf("."));
            }else{
                suffixName = ".png";
            }
            fileName = UUID.randomUUID()+suffixName;
            System.out.println(fileName);


            //获取项目路径
            Resource resource = new ClassPathResource("");
            try {
                String projectPath = resource.getFile().getAbsolutePath();
                System.out.println(projectPath);
                String relativePath="images\\personmes\\"+fileName;
                // 将img文件存入本地
                String path = projectPath + "\\static\\"+"images\\personmes\\"+ fileName;

                System.out.println(path);
                String deleteurl=personimfseritf.deletephoto(userid);
                if(deleteurl!=null) {
                    String bb[] = deleteurl.split("images");
                    String path1 = projectPath + "\\static\\" + "images" + bb[1];
                    File file1 = new File(path1);
                    file1.delete();
                }
                File dest = new File(path);

                //判断文件父目录是否存在

                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdir();
                }

                //保存文件
                try {
                    file.transferTo(dest);
                } catch (IllegalStateException | IOException e) {
                    e.printStackTrace();
                }
                personimfseritf.updataimg(userid,relativePath);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return 1;
    }
    @RequestMapping("html/personimfhtml/showhphoto.do")
    @ResponseBody
    public String showhphoto(String userid){
        return personimfseritf.showhphoto(userid);

    }
    @RequestMapping("html/personimfhtml/updatareqimf.do")
    @ResponseBody
    public int  updatareqimf(String userid,String gid){
        String muserid=personimfseritf.getmuseById(gid);
        personimfseritf.updatareqimf(muserid,userid);
        return 1;
    }
    @RequestMapping("html/personimfhtml/getjoinname.do")
    @ResponseBody
    public List<Users> getjoinname(String userid,int page){
        Users users=new Users();
        users.setMuserid(userid);
        users.setPage(page);
        int joinimf=personimfseritf.makejoinimf(userid);
        String gid=personimfseritf.getgid(userid);
        List<Users>list=personimfseritf.getjoinname(users);
        String role=personimfseritf.getrole(userid);
        List<Users>list1=personimfseritf.getgnameByuserid(users);
        Map<String,String> map=new HashMap();
        for(int i=0;i<list1.size();i++){
            map.put(list1.get(i).getUserid(),list1.get(i).getGname());
        }
        if (list.size()!=0) {
            list.get(0).setJoinimf(joinimf);
            list.get(0).setGid(gid);
            list.get(0).setRole1(role);
            for(int i=0;i<list.size();i++){
                for ( String key : map.keySet()) {
                  if(key.equals(list.get(i).getCuserid())){
                      list.get(i).setGname(map.get(key));
                  }
                }
            }
        }
        return list;
    }
    @RequestMapping("html/personimfhtml/updatames.do")
    public String updatagroupcount(String muserid,String cuserid,String gid,int page){
        personimfseritf.updatacumgid(cuserid,gid);
        personimfseritf.deletemes(muserid,cuserid);
        personimfseritf.cmupdatareqimf(cuserid,muserid,"同意");
        return "forward:getjoinname.do?userid="+muserid+"&page="+page;
    }
    @RequestMapping("html/personimfhtml/delectmse.do")
    public String delectmse(String muserid,String cuserid,int page){
        personimfseritf.deletemes(muserid,cuserid);
        personimfseritf.cmupdatareqimf(cuserid,muserid,"不同意");
        return "forward:getjoinname.do?userid="+muserid+"&page="+page;
    }
    @RequestMapping("html/personimfhtml/cmdelectmse.do")
    public String cmdelectmse(String muserid,String cuserid,int page){
        personimfseritf.deletemes(muserid,cuserid);
        return "forward:getjoinname.do?userid="+muserid+"&page="+page;
    }
    @RequestMapping("html/personimfhtml/getrole.do")
    @ResponseBody
    public String getrole(String userid){
        return personimfseritf.getrole(userid);

    }
    @RequestMapping("html/personimfhtml/updatarole.do")
    @ResponseBody
    public int updatarole(String stunumber,String userid1){
         personimfseritf.updatarole(stunumber);
         personimfseritf.outgroup(userid1);
         personimfseritf.updatarole1(userid1);
         return 1;
    }
    @RequestMapping("html/personimfhtml/selectBynum.do")
    @ResponseBody
    public int selectBynum(String stunumber) {
        Users users = personimfseritf.selectBynum(stunumber);
        if (users == null) {
            return 0;
        } else {
            return 1;
        }

    }
    @RequestMapping("html/personimfhtml/getgroupByName.do")
    @ResponseBody
    public int getgroupByName(String gname){
        String newgname=personimfseritf.getgroupByName(gname);
        if (newgname!=null){
            return 0;
        }
        else {
            return 1;
        }

    }

}
